package com.ustze.ocr.rest;

import cn.hutool.core.util.ObjectUtil;
import com.ustze.ocr.domain.res.AjaxResult;
import com.ustze.ocr.ocrtype.context.OcrTypeContext;
import com.ustze.ocr.ocrtype.enums.OcrType;
import com.ustze.ocr.ocrtype.request.OcrRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/ocr")
@RequiredArgsConstructor
public class OcrRest {


    @PostMapping("/table")
    public AjaxResult aliOcrTable(@RequestParam("file") MultipartFile file,
                                  @RequestParam("ocrCode") Integer ocrCode) {
        if (ObjectUtil.isNull(file)) {
            return AjaxResult.error("上传文件不能为空");
        }
        OcrType ocrType = OcrType.ofCode(ocrCode);
        if (null == ocrCode) {
            return AjaxResult.error("暂不支持此OCR方式");
        }
        Map<String, Object> map = OcrTypeContext.getOcrType(ocrType).ocrTable(file);
        return AjaxResult.success(map);
    }
}
